package bb.lanxing.mvvm.base;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import android.util.Log;

import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleEventObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.ViewModelStore;
import androidx.lifecycle.ViewModelStoreOwner;

import kotlin.jvm.internal.Intrinsics;



public final class VMStore implements ViewModelStoreOwner {
    private final ArrayList<LifecycleOwner> bindTargets = new ArrayList<>();
    private ViewModelStore vmStore;

    public void bindHost(final LifecycleOwner host) {
        if (!this.bindTargets.contains(host)) {
            this.bindTargets.add(host);
            host.getLifecycle().addObserver(new LifecycleEventObserver() {
                @Override
                public void onStateChanged(LifecycleOwner source, Lifecycle.Event event) {
                    ArrayList arrayList;
                    ArrayList arrayList2;
                    Object obj;
                    ViewModelStore viewModelStore;
                    Intrinsics.checkNotNullParameter(source, "source");
                    Intrinsics.checkNotNullParameter(event, "event");
                    if (event == Lifecycle.Event.ON_DESTROY) {
                        arrayList = VMStore.this.bindTargets;
                        arrayList.remove(host);
                        arrayList2 = VMStore.this.bindTargets;
                        if (!arrayList2.isEmpty()) {
                            return;
                        }
                        Set entrySet = VMStoreKt.getVMStores().entrySet();
                        Intrinsics.checkNotNullExpressionValue(entrySet, "vMStores.entries");
                        VMStore vMStore = VMStore.this;
                        Iterator it = entrySet.iterator();
                        while (true) {
                            if (!it.hasNext()) {
                                obj = null;
                                break;
                            }
                            obj = it.next();
                            if (Intrinsics.areEqual(((Map.Entry) obj).getValue(), vMStore)) {
                                break;
                            }
                        }
                        Map.Entry entry = (Map.Entry) obj;
                        if (entry == null) {
                            return;
                        }
                        viewModelStore = VMStore.this.vmStore;
                        if (viewModelStore != null) {
                            viewModelStore.clear();
                        }
                        VMStoreKt.getVMStores().remove(entry.getKey());
                        Log.d("VMStore", "onClear :: vMStores size: " + VMStoreKt.getVMStores().size() + ",  remove scope key: " + ((String) entry.getKey()));
                    }
                }
            });
        }
    }

    @Override
    public ViewModelStore getViewModelStore() {
        if (this.vmStore == null) {
            this.vmStore = new ViewModelStore();
        }
        ViewModelStore viewModelStore = this.vmStore;
        Intrinsics.checkNotNull(viewModelStore);
        return viewModelStore;
    }
}
